<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>doh.robot Slider Test</title>

	<style>
		@import "../../../../util/doh/robot/robot.css";
	</style>

	<!-- required: dojo.js -->
	<script type="text/javascript" src="../../../../dojo/dojo.js"
		djConfig="isDebug: true, parseOnLoad: true"></script>

	<script type="text/javascript">
		dojo.require("dijit.dijit"); // optimize: load dijit layer
		dojo.require("dijit.robotx");

		var arrowSliders = ['slider1','slider2','programaticSlider'];
		var arrowSliderInit_min = [0.2,10,1200];
		var arrowSliderInit_max = [99.8,90,2800];
		var accessibleSliders = ['slider1','slider2','slider3','programaticSlider']
		var accessibleSliderInit_min = [0.2,10,2,1200];
		var accessibleSliderInit_max = [99.8,90,2,2800];
		var sliderIds = ["slider1", "slider2", "slider3", "sliderH2", "programaticSlider"];

		onChange = {slider1: 'slider1input', slider2: 'slider2input', programaticSlider: 'sliderProgInput'};

		var boundaryTest = function(slider, initValue, finalValue){
			// summary:
			//		Sets slider to initValue, which should be either one below the
			//		max, or one above the min.
			//
			//		Click the arrow button once to get to the final value, and
			//		then click the arrow button again (value shouldn't change second time)
			var d = new doh.Deferred();
			slider.attr('value', initValue);
			var button = slider._descending ?
				(initValue<finalValue?slider.decrementButton: slider.incrementButton) :
				(initValue<finalValue?slider.incrementButton: slider.decrementButton);
			doh.robot.sequence(function(){
				dijit.scrollIntoView(slider.domNode);
			},500);
			doh.robot.mouseMoveAt(button, 500);
			doh.robot.mouseClick({left: true}, 500);
			doh.robot.sequence(d.getTestErrback(function(){
				doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
				if(onChange[slider.id]){
					doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
				}
				doh.robot.mouseMoveAt(button, 500);
				doh.robot.mouseClick({left: true}, 500);
				doh.robot.sequence(d.getTestCallback(function(){
					doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
					if(onChange[slider.id]){
						doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
					}
				}), 500);
			}), 500);
			return d;
		};

		var buttonHeldTest = function(slider, initValue, finalValue){
			var d = new doh.Deferred();
			slider.attr('value', initValue);
			var button = slider._descending?(initValue<finalValue?slider.decrementButton: slider.incrementButton): (initValue<finalValue?slider.incrementButton: slider.decrementButton);
			doh.robot.sequence(function(){
				dijit.scrollIntoView(slider.domNode);
			},500);
			doh.robot.mouseMoveAt(button, 500);
			doh.robot.mousePress({left: true}, 500);
			doh.robot.mouseRelease({left: true}, 5000);
			doh.robot.sequence(d.getTestCallback(function(){
				doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
				if(onChange[slider.id]){
					doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
				}
			}), 500);
			return d;
		};

		var mouseWheelTest = function(slider, initValue, finalValue){
			if(slider.discreteValues != Infinity){
				var d = new doh.Deferred();
				slider.attr('value', initValue);
				var direction = (finalValue-initValue)/Math.abs(finalValue-initValue);
				var delta = Math.ceil((slider.discreteValues-1)*Math.abs(doh.robot.mouseWheelSize)/doh.robot.mouseWheelSize);
				doh.robot.sequence(function(){
					dijit.scrollIntoView(slider.domNode);
				},500);
				doh.robot.mouseMoveAt(slider.focusNode, 500);
				doh.robot.mouseClick({left: true}, 500);
				doh.robot.mouseWheel(delta*direction, 500, Math.abs(delta)*200);
				doh.robot.sequence(d.getTestCallback(function(){
					doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
					if(onChange[slider.id]){
						doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
					}
				}), 500);
				return d;
			}
		};

		dojo.addOnLoad(function(){
			doh.robot.initRobot('../test_Slider.html');

			// Test initial values
			doh.register("initial values", function initialValues(){
				doh.is(10, dijit.byId("slider1").attr("value"), "horizontal1");
				doh.is(10, dijit.byId("slider2").attr("value"), "vertical1");
				doh.is(2, dijit.byId("slider3").attr("value"), "horizontal2");
				doh.is(10, dijit.byId("sliderH2").attr("value"), "sliderH2");
				doh.is(1400, dijit.byId("programaticSlider").attr("value"), "programaticSlider");
			});

			doh.register("attr('value', ...)", [
				function valid(){
					var slider = dijit.byId(arrowSliders[0]);
					slider.attr('value', 50);
					var d = new doh.Deferred();
					doh.robot.sequence(d.getTestCallback(dojo.hitch(this, function(){
						doh.is(50, parseFloat(dojo.byId(onChange[slider.id]).value));
					})), 500);
					return d;
				},
				function setToNull(){
					// No idea why setting to null makes it go to 0 but apparently that's expected behavior
					var slider = dijit.byId(arrowSliders[0]);
					slider.attr('value', null);
					var d = new doh.Deferred();
					doh.robot.sequence(d.getTestCallback(dojo.hitch(this, function(){
						doh.is(0, parseFloat(dojo.byId(onChange[slider.id]).value));
					})), 500);
					return d;
				}
			]);

			// Test that disabled sliders don't respond to mouse clicks (or at least,
			// the left/right arrow buttons don't)
			doh.register("disabledTest", [
				{
					name: arrowSliders[0]+'_setUp',
					slider: arrowSliders[0],
					timeout: 30000,
					runTest: function(){
						var d = new doh.Deferred();
						doh.robot.mouseMoveAt('disableButton',500);
						doh.robot.mouseClick({left: true}, 500);
						if(dojo.isMac){
							// Workaround mac/FF bug where first click doesn't register
							doh.robot.mouseClick({
								left: true
							}, 500);
						}
						doh.robot.sequence(function(){ d.callback(true); },500);
						return d;
					}
				},
				{
					name: arrowSliders[0]+'_min',
					slider: arrowSliders[0],
					value: arrowSliderInit_min[0],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return boundaryTest(this.slider, this.value, this.value);
					}
				},
				{
					name: arrowSliders[0]+'_tearDown',
					slider: arrowSliders[0],
					timeout: 30000,
					runTest: function(){
						var d = new doh.Deferred();
						doh.robot.mouseMoveAt('enableButton',500);
						doh.robot.mouseClick({left: true}, 500);
						doh.robot.sequence(function(){ d.callback(true); },500);
						return d;
					}
				}
			]);

			doh.register("buttonBoundaryTest", [
				{
					name: arrowSliders[0]+'_min',
					slider: arrowSliders[0],
					value: arrowSliderInit_min[0],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return boundaryTest(this.slider, this.value, this.slider.minimum);
					}
				},
				{
					name: arrowSliders[0]+'_max',
					slider: arrowSliders[0],
					value: arrowSliderInit_max[0],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return boundaryTest(this.slider, this.value, this.slider.maximum);
					}
				}
			]);

			doh.register("buttonHeldTest", [
				{
					name: arrowSliders[1]+'_minToMax',
					slider: arrowSliders[1],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return buttonHeldTest(this.slider, this.slider.minimum, this.slider.maximum);
					}
				},
				{
					name: arrowSliders[1]+'_maxToMin',
					slider: arrowSliders[1],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return buttonHeldTest(this.slider, this.slider.maximum, this.slider.minimum);
					}
				}
			]);

			doh.register("mouseWheelTest", [
				{
					name: accessibleSliders[1]+'_minToMax',
					slider: accessibleSliders[1],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return mouseWheelTest(this.slider, this.slider.minimum, this.slider.maximum);
					}
				},
				{
					name: accessibleSliders[1]+'_maxToMin',
					slider: accessibleSliders[1],
					timeout: 30000,
					runTest: function(){
						this.slider = dijit.byId(this.slider);
						return mouseWheelTest(this.slider, this.slider.maximum, this.slider.minimum);
					}
				}
			]);

			doh.register("general tests", [
				{
					name: "slider1",
					setUp: function(){
						dijit.byId("slider1").attr('value',10);
					},
					timeout: 4000, // this is the animated slider so there is a 500ms delay
					runTest: function(){
						var d = new doh.Deferred();
						var slider = dijit.byId("slider1");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left: true}, 500);
						// drag to 20% marker
						doh.robot.mouseMoveAt(dojo.query("div[style*='20%']", dojo.byId('dijit_form_HorizontalRule_0'))[0], 500);
						doh.robot.mouseRelease({left: true}, 500);
						doh.robot.sequence(function(){
							var value = slider.attr('value');
							if(value >= 19 && value <= 21){
								d.callback(true);
							}else{
								d.errback("slider1 failed. Expected ~20, got " + value);
							}
						}, 1400);
						return d;
					},
					tearDown: function(){
						dijit.byId("slider1").attr('value',20);
					}
				},

				{
					name: "slider2",
					timeout: 3500,
					setUp: function(){
						dijit.byId("slider2").attr('value',10);
					},
					runTest: function(){
						var d = new doh.Deferred();
						var slider = dijit.byId("slider2");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left: true}, 500);
						// drag to 20% marker (Slider is in descending order so it's 100-20=80%)
						var marker = dojo.query("div[style*='80%']", dojo.byId('dijit_form_VerticalRule_1'))[0];
						doh.robot.mouseMoveAt(marker, 500, 100, 0, 0);
						doh.robot.mouseRelease({left: true}, 500);
						doh.robot.sequence(function(){
							var value = slider.attr('value');
							if(value == 20){
								d.callback(true);
							}else{
								d.errback("slider2 failed. Expected 20, got "+value);
							}
						}, 900);
						return d;
					}
				},

				{
					name: "slider3",
					timeout: 3500,
					setUp: function(){
						dijit.byId("programaticSlider").attr('value',1000);
					},
					runTest: function(){
						var d = new doh.Deferred();
						var slider = dijit.byId("programaticSlider");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left: true}, 500);
						// drag to the top
						doh.robot.mouseMoveAt(dojo.query("div[style*='0%']", dojo.byId('dijit_form_VerticalRule_2'))[0], 500, 100, 0, 0);
						doh.robot.mouseRelease({left: true}, 500);
						doh.robot.sequence(function(){
							var value = slider.attr('value');
							if(value == 3000){
								d.callback(true);
							}else{
								d.errback("slider3 failed. Expected 3000, got "+value);
							}
						}, 900);
						return d;
					}
				}
			]);

			doh.run();
		});
	</script>
</head>
</html>
